package com.hulman.oms.service;

import com.hulman.oms.bean.Result;
import com.hulman.oms.bean.OnlyOffice;
import com.hulman.oms.dao.OnlyOfficeDao;
import com.hulman.oms.exception.IoTException;
import com.hulman.oms.util.FileUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: maxwellens
 */
@Service
public class OnlyOfficeService
{
    @Value("${file-home}")
    private String fileHome;
    @Autowired
    private OnlyOfficeDao onlyOfficeDao;

    public List<OnlyOffice> findOnlyOffices(Map<String, Object> map)
    {
        return onlyOfficeDao.findOnlyOffices(map);
    }

    public Integer findOnlyOfficesCount(Map<String, Object> map)
    {
        return onlyOfficeDao.findOnlyOfficesCount(map);
    }

    public Result findOnlyOfficesResult(Map<String, Object> map)
    {
        Integer page = (Integer) map.get("page");
        Integer limit = (Integer) map.get("limit");
        if (page != null && limit != null)
        {
            map.put("start", (page - 1) * limit);
            map.put("length", limit);
        }
        int count = onlyOfficeDao.findOnlyOfficesCount(map);
        List<OnlyOffice> data = onlyOfficeDao.findOnlyOffices(map);
        return new Result(data, count);
    }

    public OnlyOffice findOnlyOfficeById(Integer id)
    {
        return onlyOfficeDao.findOnlyOfficeById(id);
    }

    public OnlyOffice findOnlyOfficeByFileId(String fileId)
    {
        return onlyOfficeDao.findOnlyOfficeByFileId(fileId);
    }

    public void saveOnlyOffice(OnlyOffice onlyOffice)
    {
        if (onlyOffice.getId() != null && onlyOffice.getId() != 0)
        {
            onlyOfficeDao.updateOnlyOffice(onlyOffice);
        } else
        {
            onlyOfficeDao.insertOnlyOffice(onlyOffice);
        }
    }

    public void deleteOnlyOfficeById(Integer id)
    {
        onlyOfficeDao.deleteOnlyOfficeById(id);
    }

    public void deleteOnlyOffices(int[] ids)
    {
        onlyOfficeDao.deleteOnlyOffices(ids);
    }

    public OnlyOffice create(String fileId, String fileName)
    {
        OnlyOffice onlyOffice = new OnlyOffice();
        onlyOffice.setFileId(fileId);
        onlyOffice.setFileName(fileName);
        onlyOffice.setVersion(0);
        onlyOffice.setDocumentKey(FileUtil.parseFileName(fileId) + "_" + onlyOffice.getVersion());
        onlyOffice.setCreateTime(new Date());
        onlyOffice.setUpdateTime(new Date());
        onlyOfficeDao.insertOnlyOffice(onlyOffice);
        return onlyOffice;
    }

    public OnlyOffice update(String fileId, String fileUrl) throws IOException
    {
        OnlyOffice onlyOffice = onlyOfficeDao.findOnlyOfficeByFileId(fileId);
        if (onlyOffice == null)
        {
            throw new IoTException("fileId:" + fileId + "不存在");
        }
        URL url = new URL(fileUrl);
        File destFile = new File(fileHome + onlyOffice.getFileId());
        FileUtils.copyURLToFile(url, destFile);
        onlyOffice.setVersion(onlyOffice.getVersion() + 1);
        onlyOffice.setDocumentKey(FileUtil.parseFileName(fileId) + "_" + onlyOffice.getVersion());
        onlyOffice.setUpdateTime(new Date());
        onlyOfficeDao.updateOnlyOffice(onlyOffice);
        return onlyOffice;
    }
}
